package com.dream.xiaobo.dao.impl;

import com.dream.xiaobo.dao.CartDao;
import com.dream.xiaobo.entity.Cart;
import com.dream.xiaobo.entity.Product;
import com.dream.xiaobo.utils.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xiaobo
 * @date 2021/9/1 - 9:41
 */
public class CartDaoImpl implements CartDao {

    //创建queryRunner实例化对象
    private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

    /**
     * 根据商品Id和用户Id查询该商品是否存在购物车中的方法
     * @param uId
     * @param pId
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public Cart hasCart(Integer uId, String pId) throws SQLException, InvocationTargetException, IllegalAccessException {

        //根据商品Id和用户Id查询该商品是否存在购物车中的SQL
        String sql = "SELECT c.c_id AS cId, c.c_count AS cCount, c.c_num As cNum," +
                "c.u_id AS uId, c.p_id AS pId, p.p_name AS pName, p.p_time AS pTime," +
                "p.p_image AS pImage, p.p_price AS pPrice, p.p_state AS pState," +
                "p.p_info AS pInfo, p.t_id AS tId " +
                "FROM cart c JOIN product p ON c.p_id = p.p_id " +
                "WHERE c.p_id = ? AND c.u_id = ?";

        //执行SQL、将结果放在Map集合中
        Map<String, Object> map = queryRunner.query(sql, new MapHandler(), pId, uId);

        //如果不存在则返回null
        if (map == null) {
            return null;
        }

        //创建购物车实例化对象
        Cart cart = new Cart();
        //创建实例化对象
        Product product = new Product();
        //将数据库字段实体类属性映射
        BeanUtils.populate(cart,map);
        //将数据库字段实体类属性映射
        BeanUtils.populate(product,map);
        //将商品信息添加到购物车中
        cart.setProduct(product);
        //返回购物车信息
        return cart;
    }

    /**
     * 更新购物车
     * @param cart
     * @return
     * @throws SQLException
     */
    @Override
    public Integer updateCart(Cart cart) throws SQLException {

        //更新购物车的SQL
        String sql = "UPDATE cart SET c_count = ?,c_num = ? WHERE p_id = ?";
        //执行SQL
        int update = queryRunner.update(sql, cart.getcCount(), cart.getcNum(), cart.getpId());
        //返回结果
        return update;
    }

    /**
     * 添加购物车
     * @param cart
     * @return
     * @throws SQLException
     */
    @Override
    public Integer insertCart(Cart cart) throws SQLException {

        //添加的购物车的SQL
        String sql = "INSERT INTO cart(c_count,c_num,u_id,p_id) VALUES(?,?,?,?)";
        //执行SQL
        int insert = queryRunner.update(sql, cart.getcCount(), cart.getcNum(), cart.getuId(), cart.getpId());
        //返回结果
        return insert;
    }

    /**
     * 根据用户Id查询购物车中商品信息
     * @param uId
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public List<Cart> selectCartByUid(Integer uId) throws SQLException, InvocationTargetException, IllegalAccessException {

        //根据用户Id查询购物车中商品信息的SQL
        String sql = "SELECT p.p_id AS pId,p.p_name AS pName,p.p_info AS pInfo," +
                "p.p_time AS pTime,p.p_image AS pImage,p.p_price AS pPrice," +
                "p.p_state AS pState,c.c_id AS cId,c.c_num AS cNum,c.u_id AS uId " +
                "FROM cart c JOIN product p ON c.p_id = p.p_id WHERE c.u_id = ?";
        //执行SQL、将结果存放到List<Map<>>中
        List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), uId);
        //如果不存在时
        if (list == null) {
            //返回null
            return null;
        }
        //创建Cart实例化对象并存放到List集合中
        List<Cart> carts = new ArrayList<>();
        //遍历查询结果
        for (Map<String,Object> map : list){
            //创建购物车实例化对象
            Cart cart = new Cart();
            //创建商品实例化对象
            Product product = new Product();
            //将数据库字段和实体类映射
            BeanUtils.populate(cart,map);
            //将数据库字段和实体类映射
            BeanUtils.populate(product,map);
            //将商品信息存放到购物车中
            cart.setProduct(product);
            //将每一个购物车信息都添加到List集合中
            carts.add(cart);
        }
        //返回查询到的List集合内容
        return carts;
    }

    /**
     * 根据购物车Id删除方法
     * @param cId
     * @throws SQLException
     */
    @Override
    public void deleteCartByCId(String cId) throws SQLException {

        //根据购物车Id删除的SQL
        String sql = "DELETE FROM cart WHERE c_id = ?";
        //执行SQL
        queryRunner.update(sql,cId);

    }

    /**
     * 根据购物车Id更新的方法
     * @param cId
     * @param cNum
     * @param price
     * @throws SQLException
     */
    @Override
    public void updateNumANDPrice(String cId, String cNum, String price) throws SQLException {

        //根据购物车Id更新的SQL
        String sql = "UPDATE cart SET c_num = ?,c_count = ? WHERE c_id = ?";

        //类型转换
        BigDecimal numBig = new BigDecimal(cNum);
        //类型转换
        BigDecimal priceBig = new BigDecimal(price);
        //总计 = 购物车商品数量 * 每个商品的单价
        BigDecimal count = numBig.multiply(priceBig);
        //执行SQL
        queryRunner.update(sql, numBig,count ,cId);

    }

    /**
     * 根据用户Id删除的方法
     * @param uId
     * @throws SQLException
     */
    @Override
    public void deleteCartByUId(String uId) throws SQLException {

        //根据用户Id删除的SQL
        String sql = "DELETE FROM cart WHERE u_id = ?";
        //执行SQL
        queryRunner.update(sql,uId);
    }
}
